
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>如何配置Nginx以支持WebSocket协议？ - 学习卡片</title>
      <style>
        body { font-family: sans-serif; background-color: #f0f8ff; color: #333; display: flex; flex-direction: column; align-items: center; padding: 50px 20px; }
        .header h1 { font-size: 32px; }
        .grid-container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 28px; width: 100%; max-width: 1200px; }
        .card-container { perspective: 1200px; cursor: pointer; height: 250px; }
        .card { width: 100%; height: 100%; position: relative; transform-style: preserve-3d; transition: transform 0.7s; border-radius: 16px; box-shadow: 0 4px 16px rgba(0,0,0,0.08); }
        .card-container.flipped .card { transform: rotateY(180deg); }
        .card-face { position: absolute; width: 100%; height: 100%; backface-visibility: hidden; display: flex; flex-direction: column; box-sizing: border-box; border-radius: 16px; background-color: #fff; padding: 24px; }
        .card-back { background-color: #f0fff4; transform: rotateY(180deg); justify-content: space-between; }
        .card-category { font-size: 14px; color: #0052d9; margin-bottom: 8px; font-weight: 500; }
        .card-question { font-size: 20px; font-weight: 500; flex-grow: 1; display: flex; align-items: center; justify-content: center; text-align: center; }
        .card-answer-wrapper { flex-grow: 1; overflow-y: auto; }
        .card-answer { font-size: 15px; line-height: 1.7; }
        .card-footer { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 16px; margin-top: 16px; }
        .card-source { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 12px; margin-top: 12px; }
      </style>
    </head>
    <body>
      <div class="header">
        <h1>如何配置Nginx以支持WebSocket协议？ - 学习卡片</h1>
      </div>
      <div class="grid-container">
        
    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">为什么 Nginx 在作为 WebSocket 的反向代理时需要进行特殊配置？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">因为 WebSocket 协议在建立连接时使用了 HTTP/1.1 的升级（Upgrade）机制，这是一种特殊的握手机制，Nginx 必须被配置来正确处理和转发与协议升级相关的 HTTP 头部。</div>
          </div>
          <div class="card-source">来源: 1. 基本配置</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">在 Nginx 中配置 WebSocket 代理时，处理协议握手最核心的三个指令是什么？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">处理 WebSocket 协议握手最核心的三个指令是：`proxy_http_version 1.1`，`proxy_set_header Upgrade $http_upgrade`，和 `proxy_set_header Connection 'upgrade'`。</div>
          </div>
          <div class="card-source">来源: 1. 基本配置</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">特性</div>
          <div class="card-question">在 Nginx 的 WebSocket 配置中，`proxy_http_version 1.1` 指令的具体作用是什么？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">特性</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">它的作用是显式设置 Nginx 使用 HTTP/1.1 版本协议进行代理转发，因为 WebSocket 的协议升级机制是基于 HTTP/1.1 的，所以这是必须的。</div>
          </div>
          <div class="card-source">来源: 2. 详细解释</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">技术</div>
          <div class="card-question">解释 `proxy_set_header Upgrade $http_upgrade` 和 `proxy_set_header Connection 'upgrade'` 这两个指令如何协同工作以支持 WebSocket。</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">技术</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">`proxy_set_header Upgrade $http_upgrade` 负责将客户端请求中的 `Upgrade` 头部转发给后端服务器。`proxy_set_header Connection 'upgrade'` 则是将 `Connection` 头部的值设置为 `'upgrade'`。这两个头部都是 WebSocket 握手过程所必需的，用于通知后端服务器客户端请求升级协议。</div>
          </div>
          <div class="card-source">来源: 2. 详细解释</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">配置</div>
          <div class="card-question">如果需要通过 HTTPS 支持安全的 WebSocket (WSS)，Nginx 配置除了标准的 WebSocket 代理指令外，还需要添加哪些关键配置？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">配置</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">除了标准的 WebSocket 代理指令外，还需要在 `server` 块中配置监听 443 端口并启用 SSL (`listen 443 ssl;`)，并使用 `ssl_certificate` 和 `ssl_certificate_key` 指令指定 SSL 证书和密钥的路径。</div>
          </div>
          <div class="card-source">来源: 3. 支持 HTTPS 的配置</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">性能</div>
          <div class="card-question">在高并发场景下，可以通过调整文档中提到的哪三个 Nginx 指令来优化 WebSocket 的性能？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">性能</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">可以调整以下三个指令进行性能优化：1. `worker_processes`：设置 Nginx 的工作进程数。2. `worker_connections`：增加每个工作进程的最大连接数。3. `keepalive_timeout`：调整连接保持活动的超时时间。</div>
          </div>
          <div class="card-source">来源: 4. 性能优化</div>
        </div>
      </div>
    </div>

      </div>
    </body>
    </html>
